package com.zl.tby.controller;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.util.EasyExcelUtils;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyAnimalPlantQuery;
import com.zl.tby.model.TbyAnimalPlant;
import com.zl.tby.service.ITbyAnimalPlantService;
import com.zl.tby.vo.TbyAnimalPlantForm;
import com.zl.tby.vo.exp.TbyAnimalPlantExportExcel;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 动植物管理控制器
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/7 23:42
 */
@Valid
@Controller
@Mapping("/animal_plant")
public class TbyAnimalPlantController {

    @Inject
    private ITbyAnimalPlantService tbyAnimalPlantService;

    /**
     * 保存或新增
     * @param form: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save_or_update", method = MethodType.POST)
    public R<TbyAnimalPlant> saveOrUpdate(@Validated TbyAnimalPlantForm form) {
        try {
            if (form.getId() == null) {
                return tbyAnimalPlantService.add(form);
            }
            return tbyAnimalPlantService.update(form);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyAnimalPlant> get(@NotNull Long id) {
        TbyAnimalPlant entity = this.tbyAnimalPlantService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbyAnimalPlantService.delete(id);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyAnimalPlantQuery query) {
        return R.ok(this.tbyAnimalPlantService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyAnimalPlant>> list(@Body TbyAnimalPlantQuery query) {
        return R.ok(this.tbyAnimalPlantService.list(query));
    }

    /**
     * 获取概况可以使用的吴总
     * @return 列表结果
     */
    @Mapping(value = "/use_overview_list", method = MethodType.GET)
    public R<List<TbyAnimalPlant>> useOverviewList() {
        return R.ok(this.tbyAnimalPlantService.overviewList());
    }

    /**
     * 下拉选数据
     * @return 结果
     */
    @Mapping(value = "/options", method = MethodType.GET)
    public R<List<OptionType>> options() {
        return R.ok(this.tbyAnimalPlantService.options());
    }


    /**
     * 获取级联的数据，物种分类和物种
     * @return 结果
     */
    @Mapping(value = "/cascader_list", method = MethodType.GET)
    public R<List<OptionType>> cascaderList() {
        return R.ok(this.tbyAnimalPlantService.cascaderList());
    }

    /**
     * 验证名称是否存在
     * @param id: 主键id
     * @param chName: 名称
     * @return 结果
     */
    @Mapping(value = "/exist", method = MethodType.GET)
    public R<Boolean> exist(Long id, String chName) {
        return this.tbyAnimalPlantService.checkChName(id, chName);
    }

    /**
     * 下拉框的而数据，
     * @param query
     * @return
     */
    @Mapping(value = "/select_option", method = MethodType.GET)
    public R<List<OptionType>> List(@Body TbyAnimalPlantQuery query) {
        List<TbyAnimalPlant> dataList = this.tbyAnimalPlantService.list(query);
        List<OptionType> rsList = new ArrayList<>();
        for (TbyAnimalPlant entity : dataList) {
            rsList.add(new OptionType(entity.getId(), entity.getChName()));
        }
        return R.ok(rsList);
    }

    /**
     * 导出Excel
     * @param query: 查询条件
     * @param ctx：上下文
     * @throws IOException : 异常
     */
    @Mapping(value = "/excel", method = MethodType.POST)
    public void excel(@Body TbyAnimalPlantQuery query, Context ctx) throws IOException {
        List<TbyAnimalPlantExportExcel> dataList = TbyAnimalPlantExportExcel.createList(this.tbyAnimalPlantService.list(query));
        EasyExcelUtils.exportExcel(ctx, dataList, TbyAnimalPlantExportExcel.class, "物种信息", "物种信息");
    }
}
